<?php
declare (strict_types=1);

namespace app\common\model;

class ProductSpecValue extends Base
{

    protected $name = 'product_spec_value';

    public function product()
    {
        return $this->hasOne(Product::class, 'id', 'pid')
            ->field('id,title,price,cate_1,icon,stock,status,is_del,business_id,freight');
    }

    public static function getSettled($param, $user): array
    {
        try {
            if (!$param['num'] || $param['num'] < 1) {
                return ['code' => -1, 'msg' => '购买数量错误'];
            }
            $specValue = parent::where('id', $param['spec_value_id'])->find();
            if (empty($specValue)) {
                return ['code' => -1, 'msg' => '未查询到相关规格'];
            }
            if ($specValue->product->status != 1 || $specValue->product->is_del) {
                return ['code' => -1, 'msg' => '商品已下架'];
            }
            if ($param['num'] > $specValue->stock) {
                return ['code' => -1, 'msg' => '商品库存不足'];
            }
            $data['product'] = [
                [
                    'business_id'           => $specValue->product->business_id,
                    'business_title'        => $specValue->product->business->title,
                    'business_customer_tel' => $specValue->product->business->customer_tel,
                    'businessFreight'       => $specValue->product->freight * $param['num'],
                    'children'              => [
                        [
                            'pid'           => $specValue->pid,
                            'title'         => $specValue->product->title,
                            'icon'          => $specValue->product->icon,
                            'spec_value_id' => $specValue->id,
                            'vip_price'     => $specValue->vip_price,
                            'sku_name'      => SpecItem::getSpecItemName($specValue->sku_id),
                            'num'           => $param['num'],
                            'cate_1'        => $specValue->product->cate_1,
                            //'business_id'   => $specValue->product->business_id,
                        ]
                    ]
                ]
            ];
            $data['total'] = $specValue->vip_price * $param['num'];
            $data['freightTotal'] = $specValue->product->freight * $param['num'];
            $data['realTotal'] = $data['total'] + $data['freightTotal'];
            return ['code' => 1, 'msg' => 'success', 'data' => $data];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => '服务器错误：' . $e->getMessage()];
        }
    }

    public static function setSpecValue($pid, $data)
    {
        try {
            parent::destroy(['pid' => $pid]);
            $all = [];
            $vip_price = [];
            $price = [];
            $kill_price=[];
            $profit = [];
            $cost = [];
            $stock = 0;
            foreach ($data as $k => $v) {
                $key['sku_id'] = $k;
                $key['pid'] = $pid;
                $key['stock'] = $v['stock'];
                $key['price'] = $v['price'];
                $key['vip_price'] = $v['vip_price'];
                $key['kill_price'] = $v['kill_price'];
                $key['icon'] = $v['icon'];
                // if (!$business) {
                //     $key['profit'] = $v['profit'];
                //     $key['cost'] = $v['cost'];
                //     $cost[] = $v['cost'];
                //     $profit[] = $v['profit'];
                // }
                $all[] = $key;
                $stock += $v['stock'];
                $vip_price[] = $v['vip_price'];
                $price[] = $v['price'];
                $kill_price[] = $v['kill_price'];
            }
            (new self())->insertAll($all);
            return [
                // 'profit'     => $profit ? min($profit) : 0,
                // 'cost'       => $cost ? min($cost) : 0,
                'stock'      => $stock,
                'kill_price'  => min($kill_price),
                'vip_price'  => min($vip_price),
                'price'      => min($price)
            ];
        } catch (\Exception $e) {
            return false;
        }
    }

    public static function getSpecValue($pid, $business = '')
    {
        try {

            $list = parent::where('pid', $pid)->select();
            $skuData = [];
            //if (!$business) {
                foreach ($list as $v) {
                    $skuData['skus[' . $v['sku_id'] . '][stock]'] = $v['stock'];
                    $skuData['skus[' . $v['sku_id'] . '][icon]'] = $v['icon'];
                    $skuData['skus[' . $v['sku_id'] . '][price]'] = $v['price'];
                    $skuData['skus[' . $v['sku_id'] . '][vip_price]'] = $v['vip_price'];
                    $skuData['skus[' . $v['sku_id'] . '][vip_price]'] = $v['vip_price'];
                    $skuData['skus[' . $v['sku_id'] . '][kill_price]'] = $v['kill_price'];
                    $skuData['skus[' . $v['sku_id'] . '][cost]'] = $v['cost'];
                }
            // } else {

            //     foreach ($list as $v) {
            //         $sku_id = explode('-', $v['sku_id']);
            //         $sku_item = [];
            //         foreach ($sku_id as $item) {
            //             $sku_item[] = SpecItem::where('id', $item)->value('name');
            //         }
            //         $skuData[] = [
            //             'id'        => $v['id'],
            //             'sku_id'    => $v['sku_id'],
            //             'sku_item'  => implode('/', $sku_item),
            //             'price'     => $v['price'],
            //             'vip_price' => $v['vip_price'],
            //             'stock'     => $v['stock'],
            //             'icon'      => $v['icon']
            //         ];
            //     }
            // }

            return $skuData;
        } catch (\Exception $e) {

            return [];
        }
    }
}
